package com.alibaba.common;

import com.alibaba.define.enums.AppHttpCodeEnum;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author : hanyang
 * @date : 2023-10-29 19:38
 **/

@Getter
@Setter
public class ResponseResult<T> implements Serializable {

    private Integer code;

    private String msg;

    private T data;

    private ResponseResult() {
        this.code = AppHttpCodeEnum.SUCCESS.getCode();
        this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
    }

    private ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResponseResult<T> ok() {
        return new ResponseResult<>(AppHttpCodeEnum.SUCCESS.getCode(), AppHttpCodeEnum.SUCCESS.getMsg());
    }

    public static <T> ResponseResult<T> ok(T data) {
        return new ResponseResult<>(AppHttpCodeEnum.SUCCESS.getCode(), AppHttpCodeEnum.SUCCESS.getMsg(), data);
    }

    public static <T> ResponseResult<T> error(int code, String msg) {
        return new ResponseResult<>(code, msg);
    }

    public static ResponseResult<?> fromEnum(AppHttpCodeEnum appHttpCodeEnum) {
        return new ResponseResult<>(appHttpCodeEnum.getCode(), appHttpCodeEnum.getMsg());
    }

    public static ResponseResult<?> fromEnum(AppHttpCodeEnum appHttpCodeEnum, String customMsg) {
        return new ResponseResult<>(appHttpCodeEnum.getCode(), customMsg);
    }

    public ResponseResult<T> withData(T data) {
        this.data = data;
        return this;
    }
}

